import { interactive } from '../utils/interactive';
const common = {
    appId: 'wx0275cf0f74dd9c85',
    // apiurl: 'http://tapp.boshaedu.com/',
    apiurl: 'https://app.boshaedu.com/',
    // 参数： url:请求地址  param：请求参数  way：请求方式 callBack：回调函数
    urlRequest: function (url, params, methods, callback, hideLoading, noticeHandle) {
        !hideLoading && interactive.showLoading('加载中')
        uni.request({
            url: this.apiurl + url,
            method: methods,
            data: params,
            dataType: "json",
            header: {
                'Content-Type': 'application/json',
                'x-access-token': uni.getStorageSync('token')
            },
            success: (res) => {
                if (res.data.code === 0) {
                    callback(res.data)
                } else if (res.data.code === 100002) {
                    uni.removeStorageSync('token')
                    setTimeout(() => {
                        interactive.toast('token过期,请重新登陆')
                    }, 200);
                    setTimeout(() => {
                        uni.reLaunch({
                            url: '/pages/login/login'
                        });
                    }, 500);
                } else {
                    !hideLoading && !hideLoading && interactive.hideLoading();
                    setTimeout(() => {
                        interactive.toast(res.data.errMsg && res.data.errMsg || '未知异常');
                    }, 200);
                }
            },
            fail: (e) => {
                console.log('>>>>>>>>>>>>>>>>>>>>>12312', e)
                setTimeout(() => {
                    interactive.toast('服务异常，请稍后再试')
                }, 200);
                // uni.reLaunch({
                //     url: '/pages/login/login'
                // });
            },
            complete() {
                !hideLoading && interactive.hideLoading();
                uni.stopPullDownRefresh()
            }
        })
    },
    // js sort方法根据数组中对象的某一个属性值进行排序
    compare: function (attr, rule) {
        return function (a, b) {
            var val1 = a[attr];
            var val2 = b[attr];
            if (rule == 1) {
                // 降序排列
                return val2 - val1;
            } else {
                // 升序排序
                return val1 - val2;
            }
        }
    },

}

export {
    common
}